#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;


struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x): val(x), next(nullptr){}
};

class Solution{
    public:

    ListNode* ans1(ListNode* head){
        ListNode* newhead = new ListNode(0);
        newhead->next = head;
        if(!head){
            return head;
        }
        ListNode* temp = newhead;
        while(temp->next && temp->next->next){
            if(temp->next->val == temp->next->next->val){
                int val = temp->next->val;
                while(temp->next && temp->next->val == val){
                    temp->next = temp->next->next;
                }
            }else{
                temp = temp->next;
            }
        }
        return newhead->next;

    }

};

void test1(){
    vector<int> num = {1,2,3,3,4,4,5};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }

    Solution s;
    ListNode* ret = s.ans1(head);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test2(){
    vector<int> num = {1,1,1,2,3};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }

    Solution s;
    ListNode* ret = s.ans1(head);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test3(){
    vector<int> num = {1,1};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }

    Solution s;
    ListNode* ret = s.ans1(head);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}


void test4(){
    vector<int> num = {1,1,3};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }

    Solution s;
    ListNode* ret = s.ans1(head);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test5(){
    vector<int> num = {1,2,3,3};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }

    Solution s;
    ListNode* ret = s.ans1(head);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

int main(void){
    test1();
    test2();
    test3();
    test4();
    test5();

    return 0;
}